package com.peoit.honeycomb.controller;

import com.peoit.honeycomb.application.SecurityApplication;
import com.peoit.honeycomb.application.UserApplication;
import com.peoit.honeycomb.domain.User;
import com.peoit.honeycomb.domain.security.Permission;
import com.peoit.honeycomb.dto.CommonResultJsonDto;
import com.peoit.honeycomb.dto.PageCommonResultDto;
import com.peoit.honeycomb.dto.UserDto;
import com.peoit.honeycomb.dto.UserQueryDto;
import com.peoit.honeycomb.dto.security.PermissionDto;
import com.peoit.honeycomb.dto.security.PermissionPageDto;
import com.peoit.honeycomb.dto.security.PersonDto;
import com.peoit.honeycomb.dto.security.PersonPageDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2015/10/14.
 */
@Controller
@RequestMapping("/admin/permission")
public class PermissionController extends  HoneycombBaseController {

    @Autowired
    SecurityApplication securityApplication;

    @RequestMapping("/list/{offset}")
    public ModelAndView list(PermissionPageDto dto,@PathVariable int offset) {
        dto.setOffset(offset);
        List<PermissionDto> dtos = securityApplication.findpermissionPageBy(dto);

        Long count = securityApplication.findPerminssionCount(dto);
        PageCommonResultDto pageCommonResultDto = new PageCommonResultDto();
        pageCommonResultDto.setResult(dtos);
        pageCommonResultDto.setPageSize(dto.getPageSize());
        pageCommonResultDto.setCount(count);
        pageCommonResultDto.setOffset(offset);

        return view("/jsp/system/permission/permissionlist",pageCommonResultDto);
    }

    @RequestMapping("/toPermissionAdd")
    public ModelAndView toPermissionAdd() {

        List<Permission> all = (List<Permission>)this.securityApplication.findAll(Permission.class);

        return view("/jsp/system/permission/permissionadd",all);
    }

    @RequestMapping("/permissionAdd")
    public @ResponseBody  CommonResultJsonDto permissionAdd(PermissionDto permissionDto) {
        CommonResultJsonDto resultJsonDto = CommonResultJsonDto.success("/admin/permission/list/0","成功！！");


        boolean b = this.securityApplication.addPermission(permissionDto);
        if(b==false) {
            resultJsonDto = CommonResultJsonDto.fail("失败！！");
        }
        return resultJsonDto;
    }

    @RequestMapping("/toPermissionUpdate/{id}")
    public ModelAndView toPermissionUpdate(@PathVariable String id,ModelMap modelMap) {

        List<Permission> all = (List<Permission>)this.securityApplication.findAll(Permission.class);
        Permission permission = (Permission)this.securityApplication.findById(Permission.class, id);
        PermissionDto permissionDto = new PermissionDto();
        PermissionDto permissionDto1 = permissionDto.toDto(permission);
        modelMap.addAttribute("permission", permissionDto1);
        return view("/jsp/system/permission/permissionedit",all);
    }

    @RequestMapping("/permissionUpdate")
    public @ResponseBody  CommonResultJsonDto permissionUpdate(PermissionDto permissionDto) {
        CommonResultJsonDto resultJsonDto = CommonResultJsonDto.success("成功！！");


        boolean b = this.securityApplication.addPermission(permissionDto);
        if(b==false) {
            resultJsonDto = CommonResultJsonDto.fail("失败！！");
        }
        return resultJsonDto;
    }

    @RequestMapping("/deletePermission")
    public @ResponseBody CommonResultJsonDto deletePermission( String id) {
        CommonResultJsonDto resultJsonDto = CommonResultJsonDto.success("删除成功！");
        this.securityApplication.deleteBy(Permission.class, id);
        return resultJsonDto;
    }
}
